/*
 * Created by SharpDevelop.
 * User: hieutrung
 * Date: 4/22/2008
 * Time: 11:37 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace QuickWeb2CSS
{
	class MainClass
	{
		static string content;
		static string css = "";
		static string from = "<div ";
		static string to = ">";
		static string prefix = "";
		static int    styleIndex = 0;
		
		public static void Main(string[] args)
		{
			try{				
				if(Clipboard.GetDataObject().GetDataPresent(DataFormats.Text)){
	    			content = Clipboard.GetDataObject().GetData(DataFormats.UnicodeText, true).ToString();    			
	    			
	    			ArrayList arrStr = getListGroupStringInside( content, from, to);
	    			System.Console.WriteLine("div: " + arrStr.Count);
	    			convertWeb2CSS(arrStr);
	    			
	    			from = "<span ";
	    			ArrayList arrStr1 = getListGroupStringInside( content, from, to);    			
	    			System.Console.WriteLine("span: " + arrStr1.Count);
	    			convertWeb2CSS(arrStr1);
	    			
	    			Clipboard.SetDataObject(content+"\r\n\r\n"+css,true);
				}
			}catch(Exception e){System.Console.Write(e); System.Console.Read();}
			
		}
		
		//convert html to css
		public static void convertWeb2CSS(ArrayList arrStr){
			for(int i=0;i<arrStr.Count;i++){
								
				if(arrStr[i].ToString().IndexOf("style=") >= 0){
					
					string style = subStringBetween( (arrStr[i] + ">" ).Replace("\" >","\">" ), "style=\"", "\">",0);
					if(style.IndexOf("\" ") > 0) style = style.Substring(0, style.IndexOf("\" "));
					
					string id    = subStringBetween( arrStr[i] + ">", "id=\"", "\" ",0);
					if(arrStr[i].ToString().IndexOf("id=\"") == -1){
						while( prefix.Trim().Length == 0){ 
							Console.Write("Enter your default style name: ");
							prefix = Console.ReadLine();
						}
						id = prefix+"_css_" + styleIndex;
						styleIndex++;
					}
					
					//System.Console.WriteLine(style + "\r\n" + id);
					
    				//css+= "#" +id+ "{\r\n\t"+ style +"\r\n}\r\n\r\n";
    				css+= "#" +id+ "{ "+ style +" }\r\n";
    				//css+= arrStr[i]+"\r\n";
    				if(arrStr[i].ToString().IndexOf("id=\"") == -1)
    					content = content.Replace( "style=\""+style+"\"" , "id=\""+id+"\"");
    				else content = content.Replace( "style=\""+style+"\"" , "");
				}
    		}
		}
		
		//get arraylist of all string, that apdapt with this condition (from A to B)		
		public static ArrayList getListGroupString(string src, string from, string to){
			int startIndex = 0;
			ArrayList result = new ArrayList();
						
			while( startIndex >=0 ){
				string imageStr = subString( src, from, to, startIndex);				
				
				if(imageStr.Trim().Length == 0) break;
				
				startIndex = src.IndexOf( imageStr, startIndex ) + 1;				
				
				result.Add(imageStr);
			}
			return result;
		}
		
		//get arraylist of all string, that apdapt with this condition (from A to B, without A,B)		
		public static ArrayList getListGroupStringInside(string src, string from, string to){
			int startIndex = 0;
			ArrayList result = new ArrayList();
						
			while( startIndex >=0 ){
				string imageStr = subStringBetween( src, from, to, startIndex);				
				
				if(imageStr.Trim().Length == 0) break;
				
				startIndex = src.IndexOf( imageStr, startIndex ) + 1;				
				
				result.Add(imageStr);
			}
			return result;
		}
		
		//get string from string A, to string B
		//12345678 --> get from 23 to 78 --> 2345678
		public static string subString(string src, string from, string to, int fromIndex){
			string result = "";		
			
			if(src.IndexOf(from,fromIndex) >= 0 ){
				int startIndex = src.IndexOf(from,fromIndex);
				int endIndex   = src.IndexOf(to, startIndex + from.Length);
				int length	   = endIndex - startIndex + to.Length;
				if(startIndex >= 0 && length > 0)
				result = src.Substring( startIndex, length);
			}
			return result;			
		}
		
		//get string from string A, to string B but dont have A and B
		//12345678 --> get from 23 to 78 --> 456
		public static string subStringBetween(string src, string from, string to, int fromIndex){
			return subString(src,from,to,fromIndex).Replace(from,"").Replace(to,"");
		}
		
		public static void createBackup(string file){
			string StartupPath = System.Windows.Forms.Application.StartupPath;
			
			
			if(!Directory.Exists(StartupPath + "\\Backup\\File")) Directory.CreateDirectory(StartupPath + "\\Backup\\File");
			
			string filename = file.Split('\\')[file.Split('\\').Length-1];
			string filenametime = filename.Split('.')[0] + " - " + DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString() + "." + filename.Split('.')[1];
			filenametime = filenametime.Replace(":","$").Replace(",","$");
			
			System.Console.WriteLine(filenametime);
			
			File.Copy(file, StartupPath + "\\Backup\\File\\"+ filenametime, true);
			
			StreamWriter sw = new StreamWriter(StartupPath + "\\Backup\\" + filename + ".bat");
			sw.Write("Copy \"File\\"+filenametime+"\" " + file);
			sw.Flush();
			sw.Close();
			
			File.Copy(StartupPath + "\\Backup\\" + filename + ".bat", StartupPath + "\\Backup\\LastFile.bat", true);			
		}
	}
}
